package com.leetcode.LC;

import java.util.*;

public class LC187 {
    public List<String> findRepeatedDnaSequences(String s) {

        Map<String, Integer> hashMap = new HashMap<String, Integer>();
        ArrayList<String> list = new ArrayList<String>();

        for (int i = 0; i < s.length()-11; i++) {
            if (hashMap.containsKey(s.substring(i,i+10))){
                hashMap.put(s.substring(i,i+10),hashMap.get(s.substring(i,i+10))+1);
            }else {
                hashMap.put(s.substring(i, i + 10),1);
            }
        }

        for (String s1 : hashMap.keySet()) {
            if (hashMap.get(s1)>1){
                list.add(s1);
            }
        }

        return list;
    }

    public List<String> findRepeatedDnaSequences2(String s) {

        Map<String, Integer> hashMap = new HashMap<String, Integer>();
        ArrayList<String> list = new ArrayList<String>();
        String dna;

        for (int i = 0; i < s.length()-9; i++) {
            dna=s.substring(i,i+10);
            if (hashMap.containsKey(dna)){
                if (!list.contains(dna)){
                    list.add(dna);
                }
            }else {
                hashMap.put(dna,1);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        List<String> strings = new LC187().findRepeatedDnaSequences2("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT");
        for (String string : strings) {
            System.out.println(string);
        }
    }
}
